/**
 * Echarts 直角坐标系
 * @author 木人心
 * @version 1.1.2
 */
import StyleConfig from '../config'
import { EChartsOption } from 'echarts'
import { ObjectUtil } from '@utils/object-util'
import { XAXisOption, YAXisOption } from 'echarts/types/dist/shared'
import { SeriesType } from '@utils/echarts/type'

const styleConfig = new StyleConfig()

/**
 * 获取基础直角坐标系样式
 */
export function getCartesianSystemStyle (): EChartsOption {
  return {
    // 小工具
    toolbox: {
      show: true,
      feature: { saveAsImage: { title: ' ' } }
    },
    // 间距
    grid: {
      top: styleConfig.grid[0],
      right: styleConfig.grid[1],
      bottom: styleConfig.grid[2],
      left: styleConfig.grid[3],
      containLabel: true
    },
    // 图例
    legend: { textStyle: { color: styleConfig.legendTextColor } },
    // 横坐标
    xAxis: [{
      type: 'category',
      axisLine: {
        lineStyle: { color: styleConfig.xAxisLineColor }
      },
      axisLabel: { color: styleConfig.textColorBase },
      axisTick: {
        lineStyle: { color: styleConfig.textColorBase }
      },
      splitLine: { show: false }
    }],
    // 纵坐标
    yAxis: [{
      type: 'value',
      nameTextStyle: { color: styleConfig.yAxisNameColor },
      axisLine: { show: false },
      axisLabel: { color: styleConfig.yAxisTextColor },
      axisTick: { show: false },
      splitLine: {
        lineStyle: {
          color: [styleConfig.yAxisSplitLineColor],
          type: 'dashed'
        }
      }
    }],
    // 系列
    series: []
  }
}

/**
 * 直角坐标系（主要有用于bar和line）
 */
export class CartesianSystemStyle {
  currentOption: EChartsOption

  /**
   * 设置提示框
   * @param tooltip
   */
  setTooltip (tooltip: EChartsOption['tooltip'] | EChartsOption['tooltip'][]) {
    ObjectUtil.assignDeep(this.currentOption.tooltip, tooltip)
    return this
  }

  /**
   * 设置间距
   * @param grid
   */
  setGrid (grid: [number] | [number, number] | EChartsOption['grid']) {
    if (Array.isArray(grid)) {
      let t, r, b, l, x, y
      switch (grid.length) {
        case 1:
          x = y = grid[0]
          break
        case 2:
          y = grid[0]
          x = grid[1]
          break
        case 3:
          t = grid[0]
          x = grid[1]
          b = grid[2]
          break
        default:
          t = grid[0]
          r = grid[1]
          b = grid[2]
          l = grid[3]
          break
      }
      Object.assign(this.currentOption.grid, {
        top: t || y,
        right: x || r,
        bottom: y || b,
        left: x || l
      })
    } else {
      Object.assign(this.currentOption.grid, grid)
    }
    return this
  }

  /**
   * 设置图例
   * @param legend
   */
  setLegend (legend: EChartsOption['legend']) {
    Object.assign(this.currentOption.legend, legend)
    return this
  }

  /**
   * 添加一个系列
   * @param series
   */
  addSeries (series: any) {
    (this.currentOption.series as any).push(series)
    return this
  }

  /**
   * 添加一个纵轴
   * @param yAxis
   */
  addYAxis (yAxis: YAXisOption) {
    const defaultYAxis = ObjectUtil.copy(this.currentOption.yAxis[0]);
    (this.currentOption.yAxis as any).push(ObjectUtil.assignDeep(defaultYAxis, yAxis))
    return this
  }

  /**
   * 添加一个横轴
   * @param xAxis
   */
  addXAxis (xAxis: XAXisOption) {
    const defaultXAxis = ObjectUtil.copy(this.currentOption.xAxis[0]);
    (this.currentOption.xAxis as any).push(ObjectUtil.assignDeep(defaultXAxis, xAxis))
    return this
  }

  /**
   * 设置纵轴
   * @param yAxis
   * @param index
   */
  setYAxis (yAxis: YAXisOption, index: number): CartesianSystemStyle
  setYAxis (yAxis: YAXisOption | YAXisOption[]): CartesianSystemStyle
  setYAxis (yAxis: YAXisOption | YAXisOption[], index = 0) {
    if (Array.isArray(yAxis)) {
      ObjectUtil.assignDeep(this.currentOption.yAxis, yAxis)
    } else {
      ObjectUtil.assignDeep(this.currentOption.yAxis[index], yAxis)
    }
    return this
  }

  /**
   * 设置横轴
   * @param xAxis
   * @param index
   */
  setXAxis (xAxis: XAXisOption, index: number): CartesianSystemStyle
  setXAxis (xAxis: XAXisOption | XAXisOption[]): CartesianSystemStyle
  setXAxis (xAxis: XAXisOption, index = 0) {
    if (Array.isArray(xAxis)) {
      ObjectUtil.assignDeep(this.currentOption.xAxis, xAxis)
    } else {
      ObjectUtil.assignDeep(this.currentOption.xAxis[index], xAxis)
    }
    return this
  }

  /**
   * 添加缩放滑轨
   * @param start
   * @param end
   */
  addZoom (start = 0, end = 20) {
    this.currentOption.dataZoom = [
      {
        type: 'slider',
        start,
        end,
        showDataShadow: false,
        showDetail: false,
        height: 15,
        moveHandleSize: 8,
        handleIcon: 'pin',
        handleSize: 15
      }
    ]
    if ((this.currentOption.grid as any).bottom < 45) {
      (this.currentOption.grid as any).bottom = 45
    }
    return this
  }

  /**
   * 设置系列
   * @param series
   * @param index
   */
  setSeries (series: SeriesType, index: number): CartesianSystemStyle
  setSeries (series: SeriesType | SeriesType[]): CartesianSystemStyle
  setSeries (series: SeriesType | SeriesType[], index = 0) {
    if (Array.isArray(series)) {
      ObjectUtil.assignDeep(this.currentOption.series, series)
    } else {
      ObjectUtil.assignDeep(this.currentOption.series[index], series)
    }
    return this
  }

  /**
   * 获取结果
   */
  getResult () {
    return this.currentOption
  }
}
